﻿
// 2019/4/18: 首个记录
// 2019/9/19: 增加USoundSensorProperty默认构造函数
// 2020/1/14: 接口完备性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASEva;

namespace USoundSampleV1
{
    class USoundSensorProperty
    {
        public double OriginX { get; set; } // 传感器坐标原点
        public double OriginY { get; set; } // 传感器坐标原点
        public double Orientation { get; set; } // 传感器中心轴朝向
        public double AngleRangeAlpha { get; set; } // α角范围 [°]（一般在100°左右，以OriginX/Y为圆心）
        public double AngleRangeBeta { get; set; } // β角范围 [°]（一般在0~30°）
        public double DistanceRangeAlpha { get; set; } // α角对应的探测距离 [m]（较短）
        public double DistanceRangeBeta { get; set; } // β角对应的探测距离 [m]（较长）

        public USoundSensorProperty()
        {
            AngleRangeAlpha = 100;
            AngleRangeBeta = 15;
            DistanceRangeAlpha = 0.5;
            DistanceRangeBeta = 2.5;
        }
    }

    class USoundSensorData
    {
        public int ID { get; set; } // 传感器ID，从1开始
        public USoundSensorProperty Properties { get; set; } // 超声波传感器属性
        public double? Distance { get; set; } // 障碍物的距离，单位m，无障碍物则为空

        public USoundSensorData()
        {
            Properties = new USoundSensorProperty();
        }
    }

    class USoundSample : Sample
    {
        public List<USoundSensorData> DataList { get; set; } // 各个超声波传感器的数据

        public USoundSample()
        {
            DataList = new List<USoundSensorData>();
        }

        public static string Title
        {
            get
            {
                return "Ultrasound sensor count,First sensor's ID,First sensor's detected distance[m],First sensor's origin-X[m],First sensor's origin-Y[m],First sensor's orientation[°],First sensor's α angle range[°],First sensor's β angle range[°],First sensor's α distance range[m],First sensor's β distance range[m],Second sensor's ID,etc";
            }
        }

        public static string Protocol
        {
            get
            {
                return "usound-sample-v1";
            }
        }

        public static string[] Protocols
        {
            get
            {
                return new string[] { "usound-sample-v1" };
            }
        }

        public override string GetGeneralSampleProtocol()
        {
            return Protocol;
        }

        public override string[] GetGeneralSampleProtocols()
        {
            return Protocols;
        }

        public override bool FromGeneralSample(GeneralSample sample)
        {
            if (sample.Protocol == "usound-sample-v1")
            {
                var v = sample.Values.ToArray();
                if (v.Length < 1) return false;
                if (v[0].IsNotNumber()) return false;

                int sensorCount = (int)v[0].number;
                if (v.Length != 1 + sensorCount * 9) return false;

                DataList = new List<USoundSensorData>();
                for (int i = 0; i < sensorCount; i++)
                {
                    var data = new USoundSensorData();
                    data.ID = v[1 + 9 * i].ToInt().Value;
                    data.Distance = v[2 + 9 * i].ToDouble();
                    data.Properties.OriginX = v[3 + 9 * i].ToDouble().Value;
                    data.Properties.OriginY = v[4 + 9 * i].ToDouble().Value;
                    data.Properties.Orientation = v[5 + 9 * i].ToDouble().Value;
                    data.Properties.AngleRangeAlpha = v[6 + 9 * i].ToDouble().Value;
                    data.Properties.AngleRangeBeta = v[7 + 9 * i].ToDouble().Value;
                    data.Properties.DistanceRangeAlpha = v[8 + 9 * i].ToDouble().Value;
                    data.Properties.DistanceRangeBeta = v[9 + 9 * i].ToDouble().Value;
                    DataList.Add(data);
                }

                return true;
            }
            return false;
        }
    }
}
